import load
load = reload( load )

def plot_xy( self, xs, ys, name = 'default_plot',
             xValuesLabel = 'x_axis', yValuesLabel = 'y_axis',
             legendLabel = ''):
    tmp = []
    import __main__
    for i in range(len(xs)):
        x = xs[i]
        y = ys[i]
        tmp.append((x, y))
    self.xyplots.append(
        __main__.session.XYData( name = name, data = tmp,
                                 xValuesLabel = xValuesLabel,
                                 yValuesLabel = yValuesLabel,
                                 legendLabel  = legendLabel   ) )

def plot_forces( self, step_num = 2, along_edge = False ):
    import __main__
    step_name = self.step_name( step_num )
    names = []
    names.append(\
        '%s_Step_%02d_Displacement_X_Force' \
        % (self.jobname, step_num ) )
    if along_edge:
        names.append(\
            '%s_Step_%02d_Force_along_loaded_edge' \
            % (self.jobname, step_num ) )
    tmp = []
    zload = 0
    for node in self.cross_sections[-1].nodes:
        fz = node.fz[ step_name ][ - 1 ]
        zload += fz
        tmp.append( (node.theta, 0.001 * fz) )
    frmlen = len( node.fz[ step_name ] )
    tmp_displ_f = []
    fb_load = load.find_fb_load( self.zload )
    for i in range(frmlen):
        zload = self.zload[i]
        tmp_displ_f.append( (abs(self.zdisp[i]), 0.001 * zload) )
    self.xyplots.append( __main__.session.XYData(\
            name = names[ 0 ],
            data = tmp_displ_f,
            xValuesLabel = 'Displacement (absolute value), mm',
            yValuesLabel = 'Force Z, kN',
            legendLabel =  'Force Z, first buckling = %2.2f kN' \
                           % (0.001 * fb_load) ) )
    if along_edge:
        self.xyplots.append( __main__.session.XYData(\
                name = names[ 1 ],
                data = tmp,
                xValuesLabel = 'Theta, degrees',
                yValuesLabel = 'Force Z, kN',
                legendLabel =  'Total Force = %2.2f kN' \
                               % (0.001 * sum(self.zload)) ) )


def plot_displacements( self, step_num ):
    import __main__
    step_name = self.step_name( step_num )
    names = []
    numcharts = 2
    for pload in self.ploadconf.ploads:
        tmp = []
        maxdisp = 0.
        for node in pload.cross_section.nodes:
            dr = node.dr[ step_name ][-1]
            if abs(dr) > abs(maxdisp):
                maxdisp = dr
            theta = node.theta    
            tmp.append( (theta, dr) )
        names.append(\
            'CC_%02d_PLoad_%02d_Step_%02d_Displ_along_circumference' \
            % (self.index, pload.index, step_num ))
        self.xyplots.append( __main__.session.XYData(\
                name = names[ pload.index * numcharts + 0 ],
                data = tmp,
                xValuesLabel = 'Theta, degrees',
                yValuesLabel = 'Radial displacement, mm',
                legendLabel  = 'Max displacement = %f mm' % maxdisp ) )
        tmp = []
        maxdisp = 0
        for node in pload.meridian.nodes:
            dr = node.dr[ step_name ][ -1 ]
            if abs(dr) > abs(maxdisp):
                maxdisp = dr
            z = node.z    
            tmp.append( (z, dr) )
        names.append(\
            'CC_%02d_PLoad_%02d_Step_%02d_Displ_along_meridian' \
            % (self.index, pload.index, step_num ))
        self.xyplots.append( __main__.session.XYData(\
                name = names[ pload.index * numcharts + 1 ],
                data = tmp,
                xValuesLabel = 'height, mm',
                yValuesLabel = 'Radial displacement, mm',
                legendLabel  = 'Max displacement = %f mm' % maxdisp ) )

def plot_stress_analysis( self, disp_force_frame = 'DISP' ):
    keys = ['HSNFCCRT','HSNFTCRT','HSNMCCRT','HSNMTCRT']
    names = { 'HSNFCCRT': 'Hashin, Fiber Compression',
              'HSNFTCRT': 'Hashin, Fiber Tension',
              'HSNMCCRT': 'Hashin, Matrix Compression',
              'HSNMTCRT': 'Hashin, Matrix Tension' }
    dff = disp_force_frame.upper()
    if   dff == 'DISP':
        xlabel = 'Displacement (absolute value), mm'
        xaxis = [abs(i) for i in self.zdisp]
    elif dff == 'FORCE':
        xaxis = [i*0.001 for i in self.zload]
        xlabel = 'Force Z, kN'
    elif dff == 'FRAME':
        xaxis = [i for i in range(len(self.zdisp))] 
        xlabel = 'Frame index'
    for key in keys:
        xs = []; ms = []; fi = []
        for f_num in self.hashin_max_ms.keys():
            xs.append(xaxis[f_num])
            ms.append(self.hashin_max_ms[f_num][key])
            fi.append(self.hashin_max_num[f_num][key])
        name = '_'.join( [self.jobname,dff,'MS',key] )
        self.plot_xy( xs=xs, ys=ms, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Margin of Safety',
                      legendLabel  = 'MS ' + names[key] )
        name = '_'.join( [self.jobname,dff,'FI',key] )
        self.plot_xy( xs=xs, ys=fi, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Failure Index',
                      legendLabel  = 'FI ' + names[key] )
    keys = ['S11','S22','S12'] 
    for key in keys:
        xs = []; ms = []; fi = []
        for f_num in self.stress_min_ms.keys():
            xs.append(xaxis[f_num])
            ms.append(self.stress_min_ms[f_num][key])
            fi.append(self.stress_min_num[f_num][key])
        name = '_'.join( [self.jobname,dff,key,'MS','MIN'] )
        self.plot_xy( xs=xs, ys=ms, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Margin of Safety',
                      legendLabel  = 'MS ' + key )
        name = '_'.join( [self.jobname,dff,key,'STRESS','MIN'] )
        self.plot_xy( xs=xs, ys=fi, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Stress, MPa',
                      legendLabel  = 'Stress ' + key )
        xs = []; ms = []; fi = []
        for f_num in self.stress_max_ms.keys():
            xs.append(xaxis[f_num])
            ms.append(self.stress_max_ms[f_num][key])
            fi.append(self.stress_max_num[f_num][key])
        name = '_'.join( [self.jobname,dff,key,'MS','MAX'] )
        self.plot_xy( xs=xs, ys=ms, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Margin of Safety',
                      legendLabel  = 'MS ' + key )
        name = '_'.join( [self.jobname,dff,key,'STRESS','MAX'] )
        self.plot_xy( xs=xs, ys=fi, name=name,
                      xValuesLabel = xlabel,
                      yValuesLabel = 'Stress, MPa',
                      legendLabel  = 'Stress ' + key )

